package com.sumer.psychology.comment.infrastructure.messaging.rabbitmq;

import com.sumer.psychology.comment.infrastructure.persistence.mongo.MongoCommentRepository;
import com.sumer.psychology.common.port.adapter.messaging.Exchanges;
import com.sumer.psychology.common.port.adapter.messaging.rabbitmq.ExchangeListener;

/**
 * 评论创建监听
 * <p/>
 * User: zhenguang.zhu
 * Date: 2014/10/10
 * Time: 14:10
 */
public class CommentCreatedEventListener extends ExchangeListener {
    private static final String COMMENT_TYPE = "com.sumer.psychology.comment.domain.model.CommentCreatedEvent";
    private static final String COMMENT_SCORE_TYPE = "com.sumer.psychology.comment.domain.model.ScoredCommentCreatedEvent";
    private static final String COMMENT_TO_COMMENT_TYPE = "com.sumer.psychology.comment.domain.model.CommentToCommentCreatedEvent";

    private MongoCommentRepository mongoCommentRepository;

    public CommentCreatedEventListener(MongoCommentRepository mongoCommentRepository) {
        this.mongoCommentRepository = mongoCommentRepository;
    }

    @Override
    protected String exchangeName() {
        return Exchanges.DEFAULT_EXCHANGE_NAME;
    }

    @Override
    protected void filteredDispatch(String aType, String aTextMessage) {
        if (COMMENT_TO_COMMENT_TYPE.equals(aType)) {
            mongoCommentRepository().addCommentToComment(aTextMessage);
        } else {
            mongoCommentRepository().addComment(aTextMessage);
        }
    }

    @Override
    protected String[] listensTo() {
        return new String[]{
                COMMENT_TYPE,
                COMMENT_SCORE_TYPE,
                COMMENT_TO_COMMENT_TYPE};
    }

    public MongoCommentRepository mongoCommentRepository() {
        return mongoCommentRepository;
    }
}
